public class SnakeBody extends SnakePixel {
    private Darr body = new Darr();
    private Canvas canvas = null;
    public SnakeHead head = null;

    public SnakeBody() {
        super(0, 0, SnakeColor.YELLOW, '*');
    }

    public SnakeBody(int num, SnakeHead head) {
        this(num, SnakeColor.YELLOW, '*', head);
    }

    public SnakeBody(int num, int color, char ch, SnakeHead head) {
        this.canvas = head.canvas;
        this.head = head;
        set(num, color, ch);
    }

    public SnakeBody set(int num, int color, char ch) {
        super.set(0, 0, color, ch);

        Pos inc = head.getDirInc();

        while (num-- != 0) {
            if (!head.canvas.check(head.x + inc.x, head.y + inc.y))
                break;
            body.add(new Pos(head.x + inc.x, head.y + inc.y));
        }

        return this;
    }

    public boolean next() {
        return body.next();
    }

    public Pos value() {
        return (Pos)body.value();
    }

    public SnakeBody draw() {
        while (body.next()) {
            canvas.set((Pos)body.value(), this.ch);
        }
        return this;
    }

    public SnakeBody grow(int x, int y) {
        body.add(new Pos(x, y));
        return this;
    }

    public SnakeBody grow(Pos pos) {
        return grow(pos.x, pos.y);
    }

    public boolean check() {
        while (body.next()) {
            if (head.eq((Pos)body.value()))
                return true;
        }
        return false;
    }

    public SnakeBody move() {
        this.set(head.x, head.y);
        head.move();
        body.right();
        Pos first = (Pos)body.ind(0);
        first.set(x, y);
        return this;
    }
}
